package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Receivable;


public class ReceivableManager extends HibernateDaoSupport {

	public void addReceivable(Receivable receivable) {
		
		getHibernateTemplate().save(receivable);
	}
	
	public void updateReceivable(Receivable receivable) {
		
		getHibernateTemplate().update(receivable);
	}
	
	public void deleteReceivable(int id){
		
		getHibernateTemplate().delete(getReceivable(id));
	}
	
	public Receivable getReceivable(int id){
		Receivable d =(Receivable)getHibernateTemplate().get(Receivable.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<Receivable> listReceivable() {
		return getHibernateTemplate().find("from Receivable");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		List <Receivable>list = getHibernateTemplate().find("from Receivable r where r.type = '" + type + "'" );
		Receivable rv = new Receivable();
		rv.setName("总计");
		rv.setId(1999999999);
		for (Receivable receivable : list) {
			rv.setDbalance(rv.getDbalance() + receivable.getDbalance());
			rv.setRbalance(rv.getRbalance() + receivable.getRbalance());
		}
		Receivable r = new Receivable();
		r.setName("本月合计");
		rv.setId(1999999998);
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(new Date());
		String m = mm.format(new Date());
		List<Receivable> l = getHibernateTemplate().find("from  Receivable r where year(r.date) = '" + y + "' and month(r.date) = '" + m + "'and r.type = '" + type + "'" );
		for (Receivable receivable : l) {
			r.setDbalance(r.getDbalance() + receivable.getDbalance());
			r.setRbalance(r.getRbalance() + receivable.getRbalance());
		}
		r.setDbalance(fanyichu(r.getDbalance()));
		r.setRbalance(fanyichu(r.getRbalance()));
		rv.setDbalance(fanyichu(rv.getDbalance()));
		rv.setRbalance(fanyichu(rv.getRbalance()));
		list.add(r);
		list.add(rv);
		
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Receivable>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,Receivable receivable) {
		
		List <Receivable>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from Receivable r where r.type = '" + receivable.getType() + "'";
		if(!receivable.getName().equals("")){
			sql += "and r.name like '%" + receivable.getName() + "%'";
		}
		if(!receivable.getClerk().equals("")){
			
			sql += "and r.clerk = '" + receivable.getClerk() + "'";
		}
		if(receivable.getDate() != null && receivable.getEndDate() == null ){
			String d = sdf.format(receivable.getDate());
			
			sql += "and  r.date >= '" + d + "'";
		}
		if(receivable.getEndDate() != null && receivable.getDate() == null ){
			String d = sdf.format(receivable.getEndDate());
		
			sql += "and  r.date <= '" + d + "'";
		}
		if(receivable.getEndDate() != null && receivable.getDate() != null ){
			String d1 = sdf.format(receivable.getDate());
			String d2 = sdf.format(receivable.getEndDate());
			
			sql += "and r.date between '" + d1 + "' and '" +d2  +"'";
		}
		
		list = getHibernateTemplate().find(sql);
		Receivable rv = new Receivable();
		rv.setName("总计");
		rv.setId(1999999999);
		for (Receivable r : list) {
			rv.setDbalance(rv.getDbalance() + r.getDbalance());
			rv.setRbalance(rv.getRbalance() + r.getRbalance());
		}
		rv.setDbalance(fanyichu(rv.getDbalance()));
		rv.setRbalance(fanyichu(rv.getRbalance()));
		list.add(rv);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Receivable>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	public double fanyichu(double d ){
		long l =(long)(d * 100);
		
		double dd = ((double)l)/100;
		return dd;
		
	}


}



